package assignment2;
//Author Mark Mckillion, Date Completed 09/12/2011

import assignment2.Exceptions.*; //Exceptions will be used within this class.

public class ArrayStack <T> implements StackADT<T>
{   /*This class extends all of the methods within StackADT interface in order
     *to create an array style implementation of a stack.
     *See the StackADT class to find out what actions the methods perform.*/
    
    /*Create an integer to declare the initial size of the ArrayStack if the
     *user does not specify the size*/
    private final int DEFAULT_CAPACITY = 100;
    
    //Create an integer to record where the top element of the stack is.
    private int top;
    
    //Create an array which will hold the information that the user enters.
    private T [] stack;

    @SuppressWarnings("unchecked")
	public ArrayStack()
    {   //This is the default constructor, initial size: DEFAULT_CAPACITY (100).
        top = 0;
        stack = (T[])(new Object[DEFAULT_CAPACITY]);
    }
    
    @SuppressWarnings("unchecked")
	public ArrayStack(int initialCapacity)
    {   //This contructor allows the user to define the initial size.
        top = 0;
        stack = (T[])(new Object[initialCapacity]);
    }

    @Override
    public void push(T element) throws FullCollectionException
    {   //Exception occurs if the collection is full. (user cannot add objects)
        if (this.size() == stack.length)
            throw new FullCollectionException();
        
        //Top element of the stack is replaced with user input.
        stack[top] = element;
        
        //Add 1 to top to create a new point of reference.
        top++;
    }

    @Override
    public T pop() throws EmptyCollectionException
    {   //Exception occurs if the collection is empty. (user cannot remove objects)
        if (isEmpty())
             throw new EmptyCollectionException();
        
        //Minus one from top to find the newest object.
        top--;
        
        //create an instance of T and make it equal to the newest object.
        T result = stack[top];
        
        //make the newest object = null, therefore removing that item.
        stack[top] = null;
        
        //return null to confirm the removal.
        return result;
    }

    @Override
    public T peek() throws EmptyCollectionException
    {   //Exception occurs if the collection is empty. (user cannot remove objects)
        if (isEmpty())
             throw new EmptyCollectionException();
        
        //return the Object at top - 1 to find the newest item.
        return stack[top-1];
    }

    @Override
    public boolean isEmpty()
    {   //If statement which returns true if there is no Objects in the stack.
        if(top == 0)
        {   return true; 
        }   else
            {   return false;
            }
    }

    @Override
    public int size()
    {   //Finds the size of the stack by returning top.
        return top;
    }
    
    @Override
    public String toString(int elem)
    {   /*Allows the program to change the object value to a string.
         *Uses an integer value to determain what Obejct to convert*/
        String str = String.valueOf(stack[elem]);
        return str;
    }
}
